/*
 * The MIT License
 *
 * Copyright 2012 Hudson.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.logging;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * could catch system exception and report is to tapd system
 * @author janksenhu
 */
public class ExceptionReportToTapd {
    
    /**
     * report an exception to tapd  
     */
     public static void reportExceptionToTapd(Exception e){                     
           
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);        
            e.printStackTrace(pw);
            
            HttpClient httpclient= new HttpClient();
            String url="http://api.tapd.oa.com/bugs.json";
            PostMethod postmethod= new PostMethod(url);
            NameValuePair[] data={new NameValuePair("workspace_id","10024741"),     
                            new NameValuePair("reporter","janksenhu"),     
                            new NameValuePair("title",sw.toString().substring(0,(e.getMessage().length()>300?300:e.getMessage().length()))+"..."),     
                            new NameValuePair("description",sw.toString()),
                            new NameValuePair("current_owner","janksenhu"),
                            new NameValuePair("resolution","fix later"),
                            new NameValuePair("priority","high"),
                            new NameValuePair("severity","serious")
            };    
            postmethod.setRequestBody(data);
            httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
            int statuscode = 0;
            try {
                statuscode = httpclient.executeMethod(postmethod);
            } catch (Exception ex) {
                ;
            } 
    }
     
     /**
      * report a string to tapd 
      */
     public static void reportStringToTapd(String e){
             
            HttpClient httpclient= new HttpClient();
            String url="http://api.tapd.oa.com/bugs.json";
            PostMethod postmethod= new PostMethod(url);
            NameValuePair[] data={new NameValuePair("workspace_id","10024741"),     
                            new NameValuePair("reporter","janksenhu"),     
                            new NameValuePair("title",e.substring(0,(e.length()>300?300:e.length()))+"..."),     
                            new NameValuePair("description",e),
                            new NameValuePair("current_owner","janksenhu"),
                            new NameValuePair("resolution","fix later"),
                            new NameValuePair("priority","high"),
                            new NameValuePair("severity","serious")
            };    
            postmethod.setRequestBody(data);
            httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
            int statuscode = 0;
            try {
                statuscode = httpclient.executeMethod(postmethod);
            } catch (Exception ex) {
                Logger.getLogger(ExceptionReportToTapd.class.getName()).log(HudsonTestLog.level," report to tapd occured exception");
            } 
            
     }
    
     /**
      * report a string and exception to tapd 
      * the string will display in the title
      * exception's trace will diplay in the content
      */
     public static void reportStringExceptionToTapd(String e,Exception e1){
        
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);        
            e1.printStackTrace(pw);
        
            HttpClient httpclient= new HttpClient();
            String url="http://api.tapd.oa.com/bugs.json";
            PostMethod postmethod= new PostMethod(url);
            NameValuePair[] data={new NameValuePair("workspace_id","10024741"),     
                            new NameValuePair("reporter","janksenhu"),     
                            new NameValuePair("title",(e+e1.getMessage()).substring(0,((e+e1.getMessage()).length()>300?300:(e+e1.getMessage()).length()))+"..."),     
                            new NameValuePair("description",sw.toString()),
                            new NameValuePair("current_owner","janksenhu"),
                            new NameValuePair("resolution","fix later"),
                            new NameValuePair("priority","high"),
                            new NameValuePair("severity","serious")
            };    
            postmethod.setRequestBody(data);
            httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
            int statuscode = 0;
            try {
                statuscode = httpclient.executeMethod(postmethod);
            } catch (Exception ex) {
                Logger.getLogger(ExceptionReportToTapd.class.getName()).log(HudsonTestLog.level," report to tapd occured exception");
            } 
            Logger.getLogger(ExceptionReportToTapd.class.getName()).log(HudsonTestLog.level," report a exception and string to tapd call cgi http code is "+statuscode);
     }

    public static void reportThrowableToTapd(Throwable e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);        
            e.printStackTrace(pw);
            
            HttpClient httpclient= new HttpClient();
            String url="http://api.tapd.oa.com/bugs.json";
            PostMethod postmethod= new PostMethod(url);
            NameValuePair[] data={new NameValuePair("workspace_id","10024741"),     
                            new NameValuePair("reporter","janksenhu"),     
                            new NameValuePair("title",sw.toString().substring(0,(e.getMessage().length()>300?300:e.getMessage().length()))+"..."),     
                            new NameValuePair("description",sw.toString()),
                            new NameValuePair("current_owner","janksenhu"),
                            new NameValuePair("resolution","fix later"),
                            new NameValuePair("priority","high"),
                            new NameValuePair("severity","serious")
            };    
            postmethod.setRequestBody(data);
            httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
            int statuscode = 0;
            try {
                statuscode = httpclient.executeMethod(postmethod);
            } catch (Exception ex) {
                Logger.getLogger(ExceptionReportToTapd.class.getName()).log(HudsonTestLog.level," report to tapd occured exception");
            } 
            Logger.getLogger(ExceptionReportToTapd.class.getName()).log(HudsonTestLog.level," report a exception to tapd call cgi http code is "+statuscode);
    }
}
